package com.mef.smsplat.eterm;

import java.util.ArrayList;
import java.util.List;

import com.hc24.util.DateUtil;
import com.mef.smsplat.eterm.bean.MlResult;
import com.mef.smsplat.eterm.bean.MlSegment;
import com.mef.smsplat.eterm.bean.Passenger;

public class MlUtil {
//	MULTI                                                                           
//	TV9803  /23JUL          C                                                       
//	CTULXA                                                                          
//	 001   1YU/FANGZHOU      MTEVP3 F HX1   PEK1E  22JUL        O S  C              
//	 002   1AILEIHONG        MF15MT M HX4   PEK1E  22JUL        O  T C              
//	 003   1AILEIHONG        MF15MT K RR4   PEK1E  22JUL        O ST                
//	 004   1ANWENGZHAXI      NEW8KV H RR2   PEK1E  21JUL13      O ST                
//	 005   0BASANGBUZHI      NFFY32 K HX2   PEK1E  20JUL13                          
//	 006   1BASANGBUZHI      NCH72C K RR2   PEK1E  20JUL13      O ST                
//	 007   1BASANGLUOBU      MT1YW5 K HX7   PEK1E  22JUL        O ST                
//	 008   1CAOYONGHONG      MXH0VH H RR2   PEK1E  21JUL13      O ST                
//	 009   1CHENDONGMIN      MZL7G8 K RR1   PEK1E  21JUL13      O ST
	
	public static MlResult parseMl(String content){
		String[] strs=content.split("\r");
		
		String[] seconds=strs[1].split("\\s+");
		String flightNo=seconds[0];
		String date=DateUtil.ymd(seconds[1].substring(1));
		String option=seconds[2];
		String segment=strs[2].trim();
		int totalNumber=Integer.parseInt(strs[strs.length-1].split("\\s+")[2]);
		
		MlResult mlResult=new MlResult();
		mlResult.setDate(date);
		mlResult.setFlightNo(flightNo);
		mlResult.setOption(option);
		mlResult.setSegment(segment);
		mlResult.setTotalNumber(totalNumber);
		
//		if(strs[3].contains("NIL")){//没有旅客
//			List<MlSegment> mlSegmentList = new ArrayList<MlSegment>();
//			MlSegment mlSegment=new MlSegment();
//			mlSegment.setOrg(mlResult.getSegment().substring(0,3));
//			mlSegment.setDst(mlResult.getSegment().substring(3));
//			mlSegment.setPassengerList(new ArrayList<Passenger>());
//			mlSegmentList.add(mlSegment);			
//			mlResult.setMlSegmentList(mlSegmentList);
//			
//			return mlResult;
//		}
		
		List<Passenger> passengerList=new ArrayList<Passenger>();
		List<MlSegment> mlSegmentList = mlSegmentList = new ArrayList<MlSegment>();
		MlSegment mlSegment=new MlSegment();
		mlSegment.setOrg(mlResult.getSegment().substring(0,3));
		mlSegment.setDst(mlResult.getSegment().substring(3));
		for(int i=3;i<strs.length-2;i++){
			//新的航段，有经停
			if(strs[i].trim().length()==6){
				mlSegment.setPassengerList(passengerList);
				mlSegmentList.add(mlSegment);
				
				passengerList=new ArrayList<Passenger>();
				mlSegment=new MlSegment();
				mlSegment.setOrg(strs[i].substring(0,3));
				mlSegment.setDst(strs[i].substring(3));
				segment+="/"+strs[i].trim();//航段
				continue;
			}
			if(strs[i].contains("NIL")){
				mlSegment.setPassengerList(passengerList);//实际上就是一个空的旅客列表
				continue;
			}
			String[] strPass=strs[i].trim().split("\\s+");
			String no=strPass[0];
			boolean group=false;
			int groupNumber=0;
			if(no.contains("G")){
				no=no.substring(0,no.indexOf("G"));
				group=true;
				groupNumber=Integer.parseInt(strPass[4].substring(2));
			}
			String passengerName=strPass[1].substring(1);
			String pnr=strPass[2];
			String cabin=strPass[3];
			String actionCode=strPass[4];
			String office=strPass[5];
			String orderDate=DateUtil.ymd(strPass[6]);
			
			Passenger p=new Passenger();
			p.setActionCode(actionCode);
			p.setNo(no);
			p.setCabin(cabin);
			p.setDate(orderDate);
			p.setOffice(office);
			p.setPassengerName(passengerName);
			p.setPnr(pnr);
			p.setGroup(group);
			p.setGroupNumber(groupNumber);
			
			passengerList.add(p);
		}
		mlResult.setMlSegmentList(mlSegmentList);
		mlResult.setSegment(segment);
		//mlResult.setSegment(mlResult.getSegment().substring(0,mlResult.getSegment().length()-1));//去掉结尾“/”
		
		
		return mlResult;
	}
}
